﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Steering.Robot;
using System.IO.Pipes;
using System.Diagnostics;
using System.Threading;
using Xnaml;
using Simulation;
using System.Windows.Controls;
using System.Windows;

namespace Steering.Robot
{
    public class SimulationControl : RobotControl
    {
        private Virtix.SimulationGame robotSimulation;

        /// <summary>
        /// Initiates control of simulation
        /// </summary>
        /// <param name="canvas">Canvas on which simulation will be displayed</param>
        public SimulationControl(double simulationWidth, double simulationHeight) 
        {
            robotSimulation = new Virtix.SimulationGame();
            robotSimulation.Name = "Simulation";
            robotSimulation.Width = simulationWidth;
            robotSimulation.Height = simulationHeight;
        }

        public Virtix.SimulationGame RobotSimulation
        {
            get { return robotSimulation; }
            private set { robotSimulation = value; }
        }

        /// <summary>
        /// Send fork lift command to robot
        /// </summary>
        /// <param name="position">New poition of fork</param>
        public override void ControlFork(double position)
        {
            ControlForkCommand command = new ControlForkCommand(position);
            robotSimulation.SendMessage(this, command);
        }

        /// <summary>
        /// Send move command to robot
        /// </summary>
        /// <param name="power">Power given to the engines</param>
        /// <param name="tachoLimit">Distance of movement. In Degrees </param>
        /// <param name="turnRatio">Turn ratio</param>
        public override void Move(double power, double tachoLimit, double turnRatio)
        {
            MoveCommand command = new MoveCommand(power, tachoLimit, turnRatio);
            robotSimulation.SendMessage(this, command);
        }

        /// <summary>
        /// Connect robot
        /// </summary>
        /// <returns></returns>
        public override bool Connect()
        {
            Command command = new Command(CommandType.Connect);
            robotSimulation.SendMessage(this, command);
            return true;
        }

        /// <summary>
        /// Disconect robot
        /// </summary>
        public override void Disconnect()
        {
            Command commandDisconnect = new Command(CommandType.Disconnect);
            robotSimulation.SendMessage(this, commandDisconnect);

            Command commandExit = new Command(CommandType.Exit);
            robotSimulation.SendMessage(this, commandExit);
        }

        /// <summary>
        /// Idle robot
        /// </summary>
        public override void Idle()
        {
            Command commandIdleFork = new Command(CommandType.IdleFork);
            robotSimulation.SendMessage(this, commandIdleFork);

            Command commandIdleMove = new Command(CommandType.IdleMove);
            robotSimulation.SendMessage(this, commandIdleMove);
        }

        /// <summary>
        /// Idle robot fork
        /// </summary>
        public override void IdleFork()
        {
            Command command = new Command(CommandType.IdleFork);
            robotSimulation.SendMessage(this, command);
        }

        /// <summary>
        /// Idle robot movement
        /// </summary>
        public override void IdleMove()
        {
            Command command = new Command(CommandType.IdleMove);
            robotSimulation.SendMessage(this, command);
        }

    }
}
